package com.hairline.shiro;

import com.hairline.bean.Admin;
import com.hairline.bean.AdminExample;
import com.hairline.bean.Role;
import com.hairline.bean.RoleExample;
import com.hairline.bean.vo.permission.PermissionsId;
import com.hairline.mapper.AdminMapper;
import com.hairline.mapper.PermissionAllMapper;
import com.hairline.mapper.RoleMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class AdminRealm extends AuthorizingRealm {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionAllMapper permissionAllMapper;

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MallToken mallToken = (MallToken) authenticationToken;

        //根据type不同,用不同的方式处理username
        String username = mallToken.getUsername();
        AuthenticationInfo info = getInfo(username);

        return info;
    }

    private AuthenticationInfo getInfo(String username) {
        AdminExample adminExample = new AdminExample();

        //adminExample.createCriteria().andUsernameEqualTo(username);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        String password = admins.get(0).getPassword();
        //传入 username,查询到的 密码  ,以及 this.getName
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, this.getName());


        return simpleAuthenticationInfo;
    }


    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = (String) principalCollection.getPrimaryPrincipal();
        //使用mapper获取权限list
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        //获得用户
        List<Admin> admins = adminMapper.selectByExample(adminExample);

        //获得用户的roleIds
        Integer[] roleIds = admins.get(0).getRoleIds();

        //获得该用户的所有角色名
        List<String> roleNames = new ArrayList<>();
        for (Integer roleId : roleIds) {
            Role role = roleMapper.selectByPrimaryKey(roleId);
            roleNames.add(role.getName());
        }

        String[] strings = null;
        //获得该用户的所有权限
        for (Integer roleId : roleIds) {
            PermissionsId permissionsId = permissionAllMapper.selectPermissionIds(roleId);
            strings = permissionAllMapper.selectAssignedPermissions(permissionsId.getIds());
        }
        //将所有权限放进 list
        List<String> permissionList = Arrays.asList(strings);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        //权限的list传给授权器,给该用户授权
        simpleAuthorizationInfo.addStringPermissions(permissionList);



        simpleAuthorizationInfo.addRoles(roleNames);

        return simpleAuthorizationInfo;
    }


}
